#include"Sem_V2.hpp"
#include <unistd.h>

int main()
{
    //基于抽象接口类的具体建造者
    std::shared_ptr<SemaphoreBuilder> builder = std::make_shared<ConcreteSemphoreBuilder>();

    //指挥者对象
    std::shared_ptr<Director> director = std::make_shared<Director> ();

    //指挥者指导完成建造
    director->Construct(builder,BUILD_SEM,0600,3,{1,2,3});

    //完成建造创建过程，获取对象
    auto fsem = builder->GetSem();

    // sleep(10);
    srand(time(0) ^ getpid());
    if (fork() == 0)
    {
        director->Construct(builder,GET_SEM);
        auto csem = builder->GetSem();
        while (true)
        {
            csem->P(0);
            printf("C");
            fflush(stdout);
            usleep(rand() % 95270);
            printf("C ");
            usleep(rand() % 43990);
            fflush(stdout);
            csem->V(0);
        }
    }

        
    while (true)
    {
        fsem->P(0);
        printf("F");
        fflush(stdout);
        usleep(rand() % 95270);
        printf("F ");
        usleep(rand() % 43990);
        fflush(stdout);
        fsem->V(0);
    }


    return 0;
}